<?php

class Pilotage_AuthentificationController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	$this->_helper->layout()->disableLayout();
    	//$this->_helper->layout()->setLayout("authlayout");
    }

    public function indexAction()
    {
        // action body
    	// action body 
    }

    public function loginAction()
    {
        // action body
    	$this->_helper->layout()->disableLayout();
    	$this->_helper->layout()->setLayout("gestionlayout");
    	
    	$auth= new Application_Form_Authentification();
    	$this->view->form = $auth;
    	 
    	//$auth->setAction('login');
    	 
    	$this->view->FormulaireAuthentification = $auth;
    	if ($this->_request->isPost()) {
    		 
    		$formData = $this->_request->getPost();
    		 
    		if ($auth->isValid($formData)) {
    			
    			$DbAdapter=Zend_Db_Table::getDefaultAdapter();
    				
    			$authAdapter = new Zend_Auth_Adapter_DbTable($DbAdapter);
    			$authAdapter->setTableName('personnes');
    			$authAdapter->setIdentityColumn('login');
    			$authAdapter->setCredentialColumn('motpass');
    			
    			// Set the input credential values to authenticate against
    			$authAdapter->setIdentity($formData["login"]);
    			$authAdapter->setCredential($formData["motpass"]);
    			
    			// do the authentication
    			$auth = Zend_Auth::getInstance();
    			//die();
    			$result = $auth->authenticate($authAdapter);
    			
    			if ($result->isValid()) {
    				// success: store database row to auth's storage
    				// system. (Not the password though!)
    				$data = $authAdapter->getResultRowObject(null, 'password');
    				$auth->getStorage()->write($data);
    				$this->_helper->redirector("index","index","");
    			} else {
    				// failure: clear database row from session
    				$this->view->message = 'Login failed.';
    			}
    			
    			
    		}
    	}
    	$this->view->title="Authentification";
    	$this->view->form=$auth;
    }

    public function logoutAction()
    {
        // action body
    	$this->_helper->layout()->disableLayout();
     	$this->_helper->viewRenderer->setNoRender(true);
    	
    	$auth = Zend_Auth::getInstance();
    	$auth->clearIdentity();
    	$this->_helper->redirector("login","authentification","pilotage");
    }


}





